package org.tote.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.tote.model.Role;

@Repository
public class RoleDaoImpl implements RoleDao {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void save(Role role) {
		sessionFactory.getCurrentSession().save(role);
	}

	@Override
	public void update(Role role) {
		sessionFactory.getCurrentSession().merge(role);
	}

	@Override
	public void remove(Role role) {
		sessionFactory.getCurrentSession().delete(role);
	}

	public Role getById(Long id) {
		return (Role) sessionFactory.getCurrentSession().get(Role.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> getUserRoles(Long userId) {
		return sessionFactory.getCurrentSession().createQuery("FROM Role WHERE user = :userId")
				.setLong("userId", userId).list();
	}

}